﻿using System;
using System.Net;
using System.Linq;
using System.Xml.Linq;
using System.Diagnostics;


namespace TransportMe //replace with your application’s namespace
{
    public class Nimbus
    {
        public static bool completed = false;
        private string vehicleSet = "http://api.projectnimbus.org/neaodataservice.svc/VehicleSet";
        private string accountKey = "k/6DiViFKnc62/GCi9nTvg=="; //get your accountKey from http://projectnimbus.org/
        private string uniqueUserId = "a2d1c944-fb48-4cae-82bf-a17f84741bf1";// get a unique id in http://api.projectnimbus.org/tool.aspx
        WebClient svc;
        public Nimbus()
        {
            svc = new WebClient();
            svc.Headers["AccountKey"] = accountKey;
            svc.Headers["UniqueUserID"] = uniqueUserId;
            svc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(svc_DownloadStringCompleted);
        }

        //Vehicle
        void svc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                refreshCompleted();
                string rawXml = e.Result;
                XNamespace atomNS = "http://www.w3.org/2005/Atom";
                XNamespace dNS = "http://schemas.microsoft.com/ado/2007/08/dataservices";
                XNamespace mNS = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
                XNamespace gNS = "http://www.georss.org/georss";
                var results = (from item in XElement.Parse(rawXml).Descendants(atomNS + "entry")
                               let x = item.Element(atomNS + "content").Element(mNS + "properties")
                               select new Car()
                               {   
                                   VehicleID = int.Parse(x.Element(dNS + "VehicleID").Value),
                                   Make = x.Element(dNS + "Make").Value,
                                   Model = x.Element(dNS + "Model").Value,
                                   YearofManufacture = int.Parse(x.Element(dNS + "YearofManufacture").Value),
                                   EngineCapacity = int.Parse(x.Element(dNS + "EngineCapacity").Value),
                                   TypeofBody = x.Element(dNS + "TypeofBody").Value,
                                   Transmission = x.Element(dNS + "Transmission").Value,
                                   FuelType = x.Element(dNS + "FuelType").Value,
                                   FuelConsumptionUrban = double.Parse(x.Element(dNS + "FuelConsumptionUrban").Value),
                                   FuelConsumptionExtraUrban = double.Parse(x.Element(dNS + "FuelConsumptionExtraUrban").Value),
                                   FuelConsumptionCombined = double.Parse(x.Element(dNS + "FuelConsumptionCombined").Value),
                                   CO2Urban = int.Parse(x.Element(dNS + "CO2Urban").Value),
                                   CO2ExtraUrban = int.Parse(x.Element(dNS + "CO2ExtraUrban").Value),
                                   CO2Combined = int.Parse(x.Element(dNS + "CO2Combined").Value),
                                   Summary = x.Element(dNS + "Summary").Value
                               });
                foreach (Car c in results)
                {
                    Car.listOfCars.Add(c);
                }

                completed = true;

                Nimbus2 n2 = new Nimbus2();
                n2.svc.DownloadStringAsync(new Uri(n2.ERPRateSet));
            }
            else
            {
                throw new Exception("NimbusConnectionException");
            }
        }
        public void LoadDataVehicle()
        {
            svc.DownloadStringAsync(new Uri(vehicleSet));

        }

        public void refreshCompleted()
        {
            completed = false;
        }
    }
}